package com.findMoneyGame.pages {
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.display.DisplayObject;

	import com.findMoneyGame.Engine;
	import com.findMoneyGame.pages.GamePage;
	import com.findMoneyGame.pages.Wrapper;
	import com.findMoneyGame.pages.achievement.AchievementsPage;
	import com.findMoneyGame.pages.buyMoney.BuyMoneyPage;
	import com.findMoneyGame.pages.shop.PurchasingsPage;

	public class WrappingPage extends MovieClip {
		private var _engine : Engine;

		protected function get engine () { return _engine; }


		public function WrappingPage (engine : Engine) {
			activatePage();
			_engine = engine;
		}

		protected function changeState (newState : DisplayObject) {
			deactivatePage();
			stage.addChild(newState);
			removePage();
		}
		protected function showDialog (dialog : DisplayObject, center : Boolean = true) {
			deactivatePage();
			var removeDialog = function (e : Event) {
				dialog.removeEventListener(ClosablePage.CLOSE_EVENT, removeDialog);
				stage.removeChild(dialog);
				activatePage();
			};
			dialog.addEventListener(ClosablePage.CLOSE_EVENT, removeDialog);
			stage.addChild(dialog);
			if (center)
				Engine.center(dialog, this);
		}
		protected function deactivatePage () {
			var wrapper = getChildByName('wrapper');
			wrapper.purchasingsPageButton.removeEventListener(Wrapper.SHOW_PURCHASINGS_PAGE, showPurchasingsPage);
			wrapper.buyMoneyPageButton.removeEventListener(Wrapper.SHOW_BUY_MONEY_PAGE, showBuyMoneyPage);
			wrapper.achievementsPageButton.removeEventListener(Wrapper.SHOW_ACHIEVEMENTS_PAGE, showAchievementsPage);
		}
		protected function activatePage () {
			var wrapper = getChildByName('wrapper');
			wrapper.purchasingsPageButton.addEventListener(MouseEvent.CLICK, showPurchasingsPage);
			wrapper.buyMoneyPageButton.addEventListener(MouseEvent.CLICK, showBuyMoneyPage);
			wrapper.achievementsPageButton.addEventListener(MouseEvent.CLICK, showAchievementsPage);
		}
		protected function removePage () { stage.removeChild(this); }


		private function showGameLevel (e : Event) { changeState(new GamePage(engine)); }

		private function showPurchasingsPage (e : Event) { showDialog(new PurchasingsPage()); }

		private function showAchievementsPage (e : Event) { showDialog(new AchievementsPage(), false); }

		private function showBuyMoneyPage (e : Event) { showDialog(new BuyMoneyPage()); }
		
		private function showHelpPage (e : Event) { showDialog(new HelpPage()); }

		private function soundButtonClick (e : Event) {
			var wrapper = getChildByName('wrapper');
			wrapper.soundButton.turn = !wrapper.soundButton.turn;
			wrapper.soundButton.refreshActiveState();
		}
	}
}